import 'dart:convert';

import 'package:bruno/bruno.dart';
import 'package:dio/dio.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sph_shipper/Authentication/IDCard.dart';
import 'package:sph_shipper/Main/BlackList/BlackList.dart';
import 'package:sph_shipper/Main/Evaluate/MyEvaluation.dart';
import 'package:sph_shipper/Main/Insure/Insure.dart';
import 'package:sph_shipper/Main/ServiceHall/Feedback.dart';
import 'package:sph_shipper/Main/WaybillInvoicing/WaybillInvoicing.dart';
import 'package:sph_shipper/SendGoods/TMSOrderList.dart';

import '../AppConfig/Config.dart';
import '../AppConfig/LocalStorage.dart';
import '../AppConfig/RouteAct.dart';
import '../AppConfig/StringEncapsulation.dart';
import '../Authentication/FaceCheckView.dart';
import '../SendGoods/PublishEnterpriseGoods.dart';
import '../SendGoods/TMSGoodsAddView.dart';
import '../http/NoLoadingGetApi.dart';
import '../http/api.dart';
import '../l10n/messages.dart';
import 'CustomerService/CustomerServiceMain.dart';
import 'Information/InformationView.dart';
import 'MyWallet/MyWallet.dart';
import 'PlatformRules/PlatformRules.dart';
import 'Settings/SettingList.dart';

class PersonCenter extends StatefulWidget {
  const PersonCenter({super.key});

  @override
  State<PersonCenter> createState() => _PersonCenterState();
}

class _PersonCenterState extends State<PersonCenter> {
  int checkBill1=1;
  int checkBill2=1;
  Map dataMap={};
  bool isLoading=false;
  Map moneyMap={};
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getUserMessage();
    getUserMoney();
  }
  Future _onRefresh() async {
    getUserMessage();
    getUserMoney();
  }
 void getUserMessage() async {
   NoLoadinggetApiManager.getData(
     context, // 上下文对象，用于显示提示信息等
     'api/auth/shipper/user/getLoginUser', // 您的API端点
     {}, // 查询参数，如果没有可以传递null
         (responseData)  {
       setState(() {
         dataMap=responseData['data'];
       });
     },
   );
  }
  void getUserMoney(){
    NoLoadinggetApiManager.getData(
      context, // 上下文对象，用于显示提示信息等
      'api/pay/shipper/user/balanceInfo', // 您的API端点
      {}, // 查询参数，如果没有可以传递null
          (responseData)  {
        setState(() {
          if(responseData['data']!=null)
        moneyMap=responseData['data'];

        });
      },
    );
  }
  @override
  Widget build(BuildContext context) {
    var messages = AppLocalizations.of(context)!;
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      body: EasyRefresh(
        header: ClassicHeader(
          dragText: '下拉刷新',
          armedText: '准备释放',
          readyText: '加载中...',
          processingText: '加载中...',
          processedText: '刷新成功',
          noMoreText: '暂无更多',
          failedText: '刷新失败',
          messageText: '刷新时间 %T',
        ),
        onRefresh: _onRefresh,
        // onLoad: _onRefresh,
        child:   ListView(
          children: [
            Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    BrnIconButton(
                        name: '设置',
                        style: TextStyle(
                          fontSize: 13.sp,
                          color: Colors.black,
                        ),
                        direction: Direction.bottom,
                        padding: 4.h,
                        iconHeight: 18.h,
                        iconWidth: 18.w,
                        iconWidget: Image.asset("lib/Images/PersonCenter/设置icon.png"),
                        onTap: () {
                          Navigator.push( context, FadeRoute(page:SettingList()));
                        }),
                  ],
                ),
                GestureDetector(
                  child:    Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          SizedBox(width: 8.w,),
                          Container(
                            width: 60.w,
                            height: 60.h,
                            child:  dataMap['avatarPath']!=null&&dataMap['avatarPath']!=""?
                            CircleAvatar(
                              radius: 15,
                              backgroundImage: NetworkImage(MyServiceUrl.imgHeader +dataMap['avatarPath']),
                            )
                                :  CircleAvatar(
                              radius: 15,
                              backgroundImage: AssetImage("lib/Images/login/默认头像.jpeg"),
                            ),
                          ),
                          SizedBox(width: 8.w,),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              if(dataMap.isEmpty)
                                Text("货主",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w600,fontSize:16.sp ),),
                              if(dataMap.isNotEmpty)
                                Row(
                                  children: [
                                    Text(dataMap['realName']!=null?dataMap['realName']:dataMap['phoneNum'],style: TextStyle(color: Colors.black,fontWeight: FontWeight.w600,fontSize:16.sp ),),
                                    SizedBox(width: 8.w,),
                                    dataMap['authStatus']==0?
                                    Image.asset("lib/Images/PersonCenter/未认证标签.png",width: 44.w,height: 18.h,)
                                        :Image.asset("lib/Images/PersonCenter/已认证标签.png",width: 44.w,height: 18.h,),
                                    // if(dataMap['personalInfo']!=null&&compareDateWithCurrent(dataMap['personalInfo']['validTo'])!="1"&&compareDateWithCurrent(dataMap['personalInfo']['validTo'])!="4")
                                    //   BrnTagCustom.buildBorderTag(
                                    //     tagText: '有证件即将过期',
                                    //     textColor: Colors.red,
                                    //     borderColor: Colors.red,
                                    //     borderWidth: 1,
                                    //     fontSize: 12.sp,
                                    //     textPadding: EdgeInsets.all(1),
                                    //   ),
                                    // if(dataMap['personalInfo']!=null&&compareDateWithCurrent(dataMap['personalInfo']['validTo'])=="4")
                                    //   BrnTagCustom.buildBorderTag(
                                    //     tagText: '有证件已过期',
                                    //     textColor: Colors.red,
                                    //     borderColor: Colors.red,
                                    //     borderWidth: 1,
                                    //     fontSize: 10.sp,
                                    //     textPadding: EdgeInsets.all(1),
                                    //   )
                                  ],
                                ),
                              if(dataMap.isNotEmpty)
                                Text("交易"+dataMap['totalNum'].toString()+"丨"+"发货"+dataMap['goodsNum'].toString(),style: TextStyle(color: Color.fromRGBO(102, 102, 102, 1),fontSize: 12.sp),),
                              if(dataMap.isEmpty)
                                Text("交易"+"--"+"丨"+"发货"+"--".toString(),style: TextStyle(color: Color.fromRGBO(102, 102, 102, 1),fontSize: 12.sp),),
                            ],
                          ),
                        ],
                      ),
                      Column(
                        children: [
                          ((dataMap['personalInfo']!=null&&compareDateWithCurrent(dataMap['personalInfo']['validTo'])!="1"&&compareDateWithCurrent(dataMap['personalInfo']['validTo'])!="4"))
                              ?BrnTagCustom(
                            tagText: "有证件即将过期",
                            textColor: Colors.white,
                            fontSize: 9.sp,
                            backgroundColor: Colors.red,
                          ):
                          ((dataMap['personalInfo']!=null&&compareDateWithCurrent(dataMap['personalInfo']['validTo'])=="4"))
                              ?BrnTagCustom(
                            tagText: "有证件已过期",
                            textColor: Colors.white,
                            fontSize: 9.sp,
                            backgroundColor: Colors.red,
                          ):SizedBox(height: 0,),
                          GestureDetector(
                              child: Container(
                                margin: EdgeInsets.only(right: 10.w),
                                height: 20.h,
                                width: 88.w,
                                decoration: BoxDecoration(
                                    color: MyColors.themeColor,
                                    borderRadius: BorderRadius.all(Radius.circular(10))),
                                child:Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Row(
                                      children: [
                                        SizedBox(width: 8.w,),
                                        Image.asset("lib/Images/PersonCenter/钢笔icon.png",width: 13.w,height: 13.h,),
                                        SizedBox(width: 4.w,),
                                        Text(messages["编辑资料"],style: TextStyle(color: Colors.white,fontSize: 11.sp,fontWeight: FontWeight.w600),),
                                        SizedBox(width: 4.w,),
                                        Text(">",style: TextStyle(color: Colors.white,fontSize: 11.sp,fontWeight: FontWeight.w600),),
                                      ],
                                    )
                                  ],
                                ),
                              ),
                              onTap: () async {
                                if(dataMap.isNotEmpty){
                                  final result = await  Navigator.push( context, FadeRoute(page:InformationView(dataMap: dataMap,)));
                                  if (result == null) {
                                    getUserMessage();
                                  }
                                }

                              }
                          ),

                        ],
                      ),
                    ],
                  ),
                  onTap: ()async{
                    if(dataMap.isNotEmpty){
                      final result = await  Navigator.push( context, FadeRoute(page:InformationView(dataMap: dataMap,)));
                      if (result == null) {
                        getUserMessage();
                      }
                    }
                  },
                ),

               // if (dataMap['personalInfo']==null)
               //  Row(
               //    mainAxisAlignment: MainAxisAlignment.end,
               //    children: [
               //      GestureDetector(
               //        child: Text("去认证>>",style: TextStyle(color: MyColors.themeColor,fontSize: 12.sp),),
               //        onTap: () async {
               //          String? token = await TokenManager.getToken();
               //          Navigator.push( context, FadeRoute(page:IDCardVC(token: token!,)));
               //        },
               //      ),
               //      SizedBox(width: 20.w,),
               //    ],
               //  ),

                SizedBox(height: 10.h,),
                Container(

                  height: 100.h,
                  margin: EdgeInsets.only(left: 10.w,right: 10.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(10)),
                  ),
                  child: Column(
                    children: [
                      SizedBox(height: 8.h,),
                      GestureDetector(
                        child:  Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            GestureDetector(
                              child:    Row(
                                children: [
                                  SizedBox(width: 8.h,),
                                  Text(messages["我的钱包"],style: TextStyle(fontWeight: FontWeight.w600,color: Colors.black,fontSize: 14.sp),),
                                  Image.asset("lib/Images/PersonCenter/钱包icon.png",width: 15.w,height: 15.h,),
                                ],
                              ),
                              onTap: (){},
                            ),

                            Row(
                              children: [
                                Text(messages["进入钱包"],style: TextStyle(fontWeight: FontWeight.w600,color: Color.fromRGBO(154, 154, 154, 1),fontSize: 14.sp),),
                                Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                                SizedBox(width: 8.h,),
                              ],
                            ),
                          ],
                        ),
                        onTap: (){
                          Navigator.push( context, FadeRoute(page:MyWallet()));

                        },
                      ),

                      SizedBox(height: 8.h,),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          SizedBox(width: 6.h,),
                          Column(
                            children: [
                              checkBill1==1?
                              Text(moneyMap['balance']!=null? moneyMap['balance']:"0.00",style: TextStyle(fontWeight: FontWeight.w600,color: Colors.black,fontSize: 14.sp),)
                                  :Text( "*****",style: TextStyle(fontWeight: FontWeight.w600,color: Colors.black,fontSize: 14.sp),),
                              Row(
                                children: [
                                  Text("钱包余额"+"(元)",style: TextStyle(fontSize: 11.sp,color: Color.fromRGBO(154, 154, 154, 1)),),
                                  GestureDetector(
                                    child: checkBill1==1?
                                    Image.asset("lib/Images/PersonCenter/隐藏余额icon.png",width: 14.w,height: 14.h,)
                                        :Image.asset("lib/Images/PersonCenter/显示余额icon.png",width: 14.w,height: 14.h,),
                                    onTap: (){
                                      setState(() {
                                        if (checkBill1==1){
                                          checkBill1=2;
                                        }else{
                                          checkBill1=1;
                                        }
                                      });
                                    },
                                  ),

                                ],
                              ),

                            ],
                          ),
                          Image.asset("lib/Images/PersonCenter/垂直实线.png",width: 1.w,height: 46.h,),
                          Column(
                            children: [
                              checkBill2==1?
                              Text(moneyMap['frozenAmount']!=null? moneyMap['frozenAmount']:"0.00",style: TextStyle(fontWeight: FontWeight.w600,color: Colors.black,fontSize: 14.sp),)
                                  :Text( "*****",style: TextStyle(fontWeight: FontWeight.w600,color: Colors.black,fontSize: 14.sp),),
                              Row(
                                children: [
                                  Text(messages["冻结金额"]+"(元)",style: TextStyle(fontSize: 11.sp,color: Color.fromRGBO(154, 154, 154, 1)),),
                                  GestureDetector(
                                    child: checkBill2==1?
                                    Image.asset("lib/Images/PersonCenter/隐藏余额icon.png",width: 14.w,height: 14.h,)
                                        :Image.asset("lib/Images/PersonCenter/显示余额icon.png",width: 14.w,height: 14.h,),
                                    onTap: (){
                                      setState(() {
                                        if (checkBill2==1){
                                          checkBill2=2;
                                        }else{
                                          checkBill2=1;
                                        }
                                      });
                                    },
                                  ),


                                ],
                              ),
                            ],
                          ),
                          SizedBox(width: 6.h,),
                        ],
                      ),

                    ],
                  ),
                ),
                SizedBox(height: 8.h,),
                Container(

                  // height: 170.h,
                  margin: EdgeInsets.only(left: 10.w,right: 10.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(10)),

                  ),
                  child: Column(
                    children: [
                      SizedBox(height: 8.h,),
                      Row(
                        children: [
                          SizedBox(width: 15.w,),
                          Text(messages["服务大厅"],style: TextStyle(color: Colors.black,fontWeight:FontWeight.w600,fontSize: 14.sp ),)
                        ],

                      ),
                      SizedBox(height: 8.h,),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          SizedBox(width: 2.w,),
                          BrnIconButton(
                              name: messages["意见反馈"],
                              style: TextStyle(
                                fontSize: 13.sp,
                                color: Colors.black,
                              ),
                              direction: Direction.bottom,
                              padding: 4.h,
                              iconHeight: 26.h,
                              iconWidth: 26.w,
                              iconWidget: Image.asset("lib/Images/PersonCenter/意见反馈icon.png",),
                              onTap: () {
                                Navigator.push( context, FadeRoute(page:FeedBack()));
                              }),
                          BrnIconButton(
                              name: messages["保险"],
                              style: TextStyle(
                                fontSize: 13.sp,
                                color: Colors.black,
                              ),
                              direction: Direction.bottom,
                              padding: 4.h,
                              iconHeight: 26.h,
                              iconWidth: 26.w,
                              iconWidget: Image.asset("lib/Images/PersonCenter/保险icon.png"),
                              onTap: () {
                                Navigator.push( context, FadeRoute(page:Insure()));
                              }),
                          BrnIconButton(
                              name: messages["客户服务"],
                              style: TextStyle(
                                fontSize: 13.sp,
                                color: Colors.black,
                              ),
                              direction: Direction.bottom,
                              padding: 4.h,
                              iconHeight: 26.h,
                              iconWidth: 26.w,
                              iconWidget: Image.asset("lib/Images/PersonCenter/客户服务icon.png"),
                              onTap: () {
                                Navigator.push( context, FadeRoute(page:CustomerServiceMain()));
                              }),
                          BrnIconButton(
                              name: messages["我的评价"],
                              style: TextStyle(
                                fontSize: 13.sp,
                                color: Colors.black,
                              ),
                              direction: Direction.bottom,
                              padding: 4.h,
                              iconHeight: 26.h,
                              iconWidth: 26.w,
                              iconWidget: Image.asset("lib/Images/PersonCenter/我的评价icon.png"),
                              onTap: () {
                                Navigator.push( context, FadeRoute(page:MyEvaluation(userMap: dataMap,)));
                              }),
                          SizedBox(width: 2.w,),
                        ],
                      ),
                      SizedBox(height: 8.h,),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          SizedBox(width: 2.w,),
                          BrnIconButton(
                              name: messages["黑名单"],
                              style: TextStyle(
                                fontSize: 13.sp,
                                color: Colors.black,
                              ),
                              direction: Direction.bottom,
                              padding: 4.h,
                              iconHeight: 26.h,
                              iconWidth: 26.w,
                              iconWidget: Image.asset("lib/Images/PersonCenter/黑名单icon.png"),
                              onTap: () {
                                Navigator.push( context, FadeRoute(page:BlackList()));
                              }),
                          BrnIconButton(
                              name: messages["运单开票"],
                              style: TextStyle(
                                fontSize: 13.sp,
                                color: Colors.black,
                              ),
                              direction: Direction.bottom,
                              padding: 4.h,
                              iconHeight: 26.h,
                              iconWidth: 26.w,
                              iconWidget: Image.asset("lib/Images/PersonCenter/运单开票icon.png"),
                              onTap: () {
                                Navigator.push( context, FadeRoute(page:WaybillInvoicing()));
                              }),

                          BrnIconButton(
                              name: messages["平台规则"],
                              style: TextStyle(
                                fontSize: 13.sp,
                                color: Colors.black,
                              ),
                              direction: Direction.bottom,
                              padding: 4.h,
                              iconHeight: 26.h,
                              iconWidth: 26.w,
                              iconWidget: Image.asset("lib/Images/PersonCenter/平台规则icon.png"),
                              onTap: () {
                                Navigator.push( context, FadeRoute(page:PlatformRules()));
                              }),
                          BrnIconButton(
                              name: "",
                              style: TextStyle(
                                fontSize: 13.sp,
                                color: Colors.black,
                              ),
                              direction: Direction.bottom,
                              padding: 4.h,
                              iconHeight: 26.h,
                              iconWidth: 26.w,
                              // iconWidget: Image.asset("lib/Images/PersonCenter/平台规则icon.png"),
                              onTap: () {

                              }),

                          SizedBox(width: 2.w,),

                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
                SizedBox(height: 8.h,),
            if(dataMap['companyInfo']!=null)
            Container(

              margin: EdgeInsets.only(left: 10.w,right: 10.w),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10)),
              ),
              child:Column(
                children: [
                  SizedBox(height: 8.h,),
                  Row(
                    children: [
                      SizedBox(width: 15.w,),
                      Text("企业服务",style: TextStyle(color: Colors.black,fontWeight:FontWeight.w600,fontSize: 14.sp ),)
                    ],

                  ),
                  SizedBox(height: 8.h,),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      SizedBox(width: 2.w,),
                      // BrnIconButton(
                      //     name:"创建订单",
                      //     style: TextStyle(
                      //       fontSize: 13.sp,
                      //       color: Colors.black,
                      //     ),
                      //     direction: Direction.bottom,
                      //     padding: 4.h,
                      //     iconHeight: 26.h,
                      //     iconWidth: 26.w,
                      //     iconWidget: Image.asset("lib/Images/PersonCenter/黑名单icon.png"),
                      //     onTap: () {
                      //       Navigator.push( context, FadeRoute(page:TMSGoodsAddView()));
                      //     }),
                      BrnIconButton(
                          name: "管理订单",
                          style: TextStyle(
                            fontSize: 13.sp,
                            color: Colors.black,
                          ),
                          direction: Direction.bottom,
                          padding: 4.h,
                          iconHeight: 26.h,
                          iconWidth: 26.w,
                          iconWidget: Image.asset("lib/Images/PersonCenter/运单管理.png"),
                          onTap: () {
                            Navigator.push( context, FadeRoute(page:TMSOrderList()));
                          }),
                      BrnIconButton(
                          name: "",
                          style: TextStyle(
                            fontSize: 13.sp,
                            color: Colors.black,
                          ),
                          direction: Direction.bottom,
                          padding: 4.h,
                          iconHeight: 26.h,
                          iconWidth: 26.w,
                          // iconWidget: Image.asset("lib/Images/PersonCenter/平台规则icon.png"),
                          onTap: () {

                          }),
                      BrnIconButton(
                          name: "",
                          style: TextStyle(
                            fontSize: 13.sp,
                            color: Colors.black,
                          ),
                          direction: Direction.bottom,
                          padding: 4.h,
                          iconHeight: 26.h,
                          iconWidth: 26.w,
                          // iconWidget: Image.asset("lib/Images/PersonCenter/平台规则icon.png"),
                          onTap: () {

                          }),
                      BrnIconButton(
                          name: "",
                          style: TextStyle(
                            fontSize: 13.sp,
                            color: Colors.black,
                          ),
                          direction: Direction.bottom,
                          padding: 4.h,
                          iconHeight: 26.h,
                          iconWidth: 26.w,
                          // iconWidget: Image.asset("lib/Images/PersonCenter/平台规则icon.png"),
                          onTap: () {

                          }),

                      SizedBox(width: 2.w,),

                    ],
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
